<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\partner\controller;

use app\admin\widget\Name;
use app\common\controller\PartnerBaseController;
use app\common\enum\AssetPayType;
use app\common\enum\HolderType;
use app\common\enum\HouseStatus;
use app\common\enum\HouseType;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsDelete;
use app\common\enum\IsHistory;
use app\common\enum\IsHouse;
use app\common\enum\IsMortgage;
use app\common\ext\IDb;
use app\common\ext\User;

class UserHouse extends PartnerBaseController {

    /**
     * @功能：个人房产列表
     * @开发者：cxl
     */
    public function house_list(){
        //接收参数
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        //判断房产是否存在
        $user_house_where['is_delete'] = IsDelete::No;
        $user_house_where['is_history'] = IsHistory::not;
        $user_house_where['user_id'] = $user_id;
        $user_house_list = IDb::getInstance("asset_house")->setDbWhere($user_house_where)->setDbOrder("user_house_id")->sel();
        if(empty($user_house_list) || !is_array($user_house_list)){
            $user_house_list = array();
        }

        //设置省市区
        foreach($user_house_list as &$data){
            if(!empty($data['province_id'])) $data['province_name'] = Name::area($data['province_id']);
            if(!empty($data['city_id'])) $data['city_name'] = Name::area($data['city_id']);
            if(!empty($data['area_id'])) $data['area_name'] = Name::area($data['area_id']);

            if($data['holder_type'] != HolderType::Other){
                $data['holder_name'] = get_enum_name('holder_type',$data['holder_type']);
            }
            if($data['house_type'] != HouseType::other){
                $data['house_type_name'] = get_enum_name('house_type',$data['house_type']);
            }
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_house_list",$user_house_list);
        self::send();
    }

    /**
     * @功能：添加个人房产
     * @开发者：cxl
     */
    public function house_add(){
        //接收参数
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');
        $holder_type  = self::get_data('holder_type',self::INT,'请选择房产所有人');
        $holder_name  = self::get_data('holder_name');
        $house_type  = self::get_data('house_type',self::INT,'请选择房产类型');
        $house_type_name  = self::get_data('house_type_name');
        $address  = self::get_param('address',true,self::NOT_EMPTY,'房产地址');
        $area  = self::get_param('area',true,self::DOUBLE,'房产面积');
        $price  = self::get_param('price',true,self::DOUBLE,'房产估价');
        $service_life  = self::get_param('service_life',true,self::INT,'使用年限');
        $pay_type  = self::get_data('pay_type',self::INT,'请选择是否有按揭');

        $is_mortgage  = self::get_data('is_mortgage',self::INT,'请选择是否有抵押');
        $licence_url  = self::get_data('licence_url');

        //长度限制
        $price = self::data_filter($price,self::DOUBLE,'房产估值');
        $area = self::data_filter($area,self::DOUBLE,'房产面积');
        $service_life = self::data_filter($service_life,self::INT,'使用年限');

        //判断是否设置空值
        if(trim($house_type_name) == "") $house_type_name = null;
        if(trim($holder_name) == "") $holder_name = null;

        if($holder_type == HolderType::Other){
            //所有人为其它时取填写的值
            $user_house_data['holder_name'] = $holder_name;
        }

        if($house_type == HouseType::other){
            //房产类型为其它时取填写的值
            $user_house_data['house_type_name'] = $house_type_name;
        }

        if($pay_type == AssetPayType::installment){
            //数据接收
            $installment_loan_money  = self::get_param('installment_loan_money',true,self::NOT_EMPTY,'贷款金额');
            $installment_pay_type  = self::get_param('installment_pay_type',true,self::NOT_EMPTY,'还款方式');
            $installment_loan_cycle  = self::get_param('installment_loan_cycle',true,self::NOT_EMPTY,'贷款周期');
            $installment_start_date  = self::get_param('installment_start_date',true,self::NOT_EMPTY,'起始日期');
            $installment_end_date  = self::get_param('installment_end_date',true,self::NOT_EMPTY,'结清日期');

            //数据检测
            $installment_loan_money = self::data_filter($installment_loan_money,self::INT,'贷款金额');
            $installment_loan_cycle = self::data_filter($installment_loan_cycle,self::INT_TINY,'贷款周期');

            if(trim($installment_start_date) == "") $installment_start_date = null;
            if(trim($installment_end_date) == "") $installment_end_date = null;

            //按揭购买
            $user_house_data['installment_loan_money'] = $installment_loan_money;
            $user_house_data['installment_pay_type'] = $installment_pay_type;
            $user_house_data['installment_loan_cycle'] = $installment_loan_cycle;
            $user_house_data['installment_start_date'] = $installment_start_date;
            $user_house_data['installment_end_date'] = $installment_end_date;
        }else{
            //全款购买
            $pay_type = AssetPayType::full;

            $user_house_data['installment_loan_money'] = ['exp','null'];
            $user_house_data['installment_pay_type'] = ['exp','null'];
            $user_house_data['installment_loan_cycle'] = ['exp','null'];
            $user_house_data['installment_start_date'] = ['exp','null'];
            $user_house_data['installment_end_date'] = ['exp','null'];
        }

        if($is_mortgage == IsMortgage::yes){
            //房产已抵押
            $mortgage_money = self::get_param('mortgage_money',true,self::DOUBLE,'抵押金额');
            $mortgage_pay_type = self::get_param('mortgage_pay_type',true,self::NOT_EMPTY,'还款方式');
            $mortgage_start_time  = self::get_param('mortgage_start_time',true,self::NOT_EMPTY,'起始日期');
            $mortgage_end_time  = self::get_param('mortgage_end_time',true,self::NOT_EMPTY,'结清日期');

//            $mortgage_money = self::data_filter($mortgage_money,self::DOUBLE,'抵押金额');

            if(trim($mortgage_start_time) == "") $mortgage_start_time = null;
            if(trim($mortgage_end_time) == "") $mortgage_end_time = null;

            $user_house_data['mortgage_money'] = floatval($mortgage_money);
            $user_house_data['mortgage_pay_type'] = floatval($mortgage_pay_type);
            $user_house_data['mortgage_start_time'] = $mortgage_start_time;
            $user_house_data['mortgage_end_time'] = $mortgage_end_time;
        }else{
            //房产未抵押
            $is_mortgage = IsMortgage::not;
            $user_house_data['mortgage_money'] = ['exp','null'];
            $user_house_data['mortgage_pay_type'] = ['exp','null'];
            $user_house_data['mortgage_start_time'] = ['exp','null'];
            $user_house_data['mortgage_end_time'] = ['exp','null'];
        }

        //添加房产
        $user_house_data['user_id'] = $user_id;
        $user_house_data['address'] = $address;
        $user_house_data['service_life'] = $service_life;
        $user_house_data['area'] = $area;
        $user_house_data['price'] = $price;
        $user_house_data['holder_type'] = $holder_type;
        $user_house_data['house_type'] = $house_type;
        $user_house_data['pay_type'] = $pay_type;
        $user_house_data['is_mortgage'] = $is_mortgage;
        $user_house_data['licence_url'] = $licence_url;
        $user_house_data['house_status'] = HouseStatus::Wait;
        $user_house_data['is_history'] = IsHistory::not;
        $user_house_data['create_time'] = get_date_time();

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        //开启事物
        IDb::dbStartTrans();

        //取得房产列表
        $user_house_add = IDb::getInstance("asset_house")->setDbData($user_house_data)->add();

        //判断添加房产是否正常
        if($user_house_add === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0001);
            self::set_msg("添加房产信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_HOUSE,IsHouse::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("添加房产信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_house_id",$user_house_add);
        self::send();
    }

    /**
     * @功能：编辑个人房产
     * @开发者：cxl
     */
    public function house_edit(){
        //接收参数
        $user_house_id  = self::get_data('user_house_id',self::NOT_EMPTY,"房产型号编码不能为空！");
        $holder_type  = self::get_data('holder_type',self::INT,'请选择房产所有人');
        $holder_name  = self::get_data('holder_name');
        $house_type  = self::get_data('house_type',self::INT,'请选择房产类型');
        $house_type_name  = self::get_data('house_type_name');
        $address  = self::get_param('address',true,self::NOT_EMPTY,'房产地址');
        $area  = self::get_param('area',true,self::DOUBLE,'房产面积');
        $price  = self::get_param('price',true,self::DOUBLE,'房产估价');
        $service_life  = self::get_param('service_life',true,self::INT,'使用年限');
        $pay_type  = self::get_data('pay_type',self::INT,'请选择是否有按揭');

        $is_mortgage  = self::get_data('is_mortgage',self::INT,'请选择是否有抵押');
        $licence_url  = self::get_data('licence_url');

        //长度限制
        $price = self::data_filter($price,self::DOUBLE,'房产估值');
        $area = self::data_filter($area,self::DOUBLE,'房产面积');
        $service_life = self::data_filter($service_life,self::INT,'使用年限');

        //判断是否设置空值
        if(trim($house_type_name) == "") $house_type_name = null;
        if(trim($holder_name) == "") $holder_name = null;

        if($holder_type == HolderType::Other){
            //所有人为其它时取填写的值
            $user_house_data['holder_name'] = $holder_name;
        }

        if($house_type == HouseType::other){
            //房产类型为其它时取填写的值
            $user_house_data['house_type_name'] = $house_type_name;
        }

        if($pay_type == AssetPayType::installment){
            //数据接收
            $installment_loan_money  = self::get_param('installment_loan_money',true,self::NOT_EMPTY,'贷款金额');
            $installment_pay_type  = self::get_param('installment_pay_type',true,self::NOT_EMPTY,'还款方式');
            $installment_loan_cycle  = self::get_param('installment_loan_cycle',true,self::NOT_EMPTY,'贷款周期');
            $installment_start_date  = self::get_param('installment_start_date',true,self::NOT_EMPTY,'起始日期');
            $installment_end_date  = self::get_param('installment_end_date',true,self::NOT_EMPTY,'结清日期');

            //数据检测
            $installment_loan_money = self::data_filter($installment_loan_money,self::INT,'贷款金额');
            $installment_loan_cycle = self::data_filter($installment_loan_cycle,self::INT_TINY,'贷款周期');

            if(trim($installment_start_date) == "") $installment_start_date = null;
            if(trim($installment_end_date) == "") $installment_end_date = null;

            //按揭购买
            $user_house_data['installment_loan_money'] = $installment_loan_money;
            $user_house_data['installment_pay_type'] = $installment_pay_type;
            $user_house_data['installment_loan_cycle'] = $installment_loan_cycle;
            $user_house_data['installment_start_date'] = $installment_start_date;
            $user_house_data['installment_end_date'] = $installment_end_date;
        }else{
            //全款购买
            $pay_type = AssetPayType::full;

            $user_house_data['installment_loan_money'] = ['exp','null'];
            $user_house_data['installment_pay_type'] = ['exp','null'];
            $user_house_data['installment_loan_cycle'] = ['exp','null'];
            $user_house_data['installment_start_date'] = ['exp','null'];
            $user_house_data['installment_end_date'] = ['exp','null'];
        }

        if($is_mortgage == IsMortgage::yes){
            //房产已抵押
            $mortgage_money = self::get_param('mortgage_money',true,self::DOUBLE,'抵押金额');
            $mortgage_pay_type = self::get_param('mortgage_pay_type',true,self::NOT_EMPTY,'还款方式');
            $mortgage_start_time  = self::get_param('mortgage_start_time',true,self::NOT_EMPTY,'起始日期');
            $mortgage_end_time  = self::get_param('mortgage_end_time',true,self::NOT_EMPTY,'结清日期');

//            $mortgage_money = self::data_filter($mortgage_money,self::DOUBLE,'抵押金额');

            if(trim($mortgage_start_time) == "") $mortgage_start_time = null;
            if(trim($mortgage_end_time) == "") $mortgage_end_time = null;

            $user_house_data['mortgage_money'] = floatval($mortgage_money);
            $user_house_data['mortgage_pay_type'] = floatval($mortgage_pay_type);
            $user_house_data['mortgage_start_time'] = $mortgage_start_time;
            $user_house_data['mortgage_end_time'] = $mortgage_end_time;
        }else{
            //房产未抵押
            $is_mortgage = IsMortgage::not;
            $user_house_data['mortgage_money'] = ['exp','null'];
            $user_house_data['mortgage_pay_type'] = ['exp','null'];
            $user_house_data['mortgage_start_time'] = ['exp','null'];
            $user_house_data['mortgage_end_time'] = ['exp','null'];
        }

        //添加房产
//        $user_house_data['province_id'] = $province_id;
//        $user_house_data['city_id'] = $city_id;
//        $user_house_data['area_id'] = $area_id;
        $user_house_data['address'] = $address;
        $user_house_data['service_life'] = $service_life;
        $user_house_data['area'] = $area;
        $user_house_data['price'] = $price;
        $user_house_data['holder_type'] = $holder_type;
        $user_house_data['house_type'] = $house_type;
        $user_house_data['pay_type'] = $pay_type;
        $user_house_data['is_mortgage'] = $is_mortgage;
        $user_house_data['licence_url'] = $licence_url;

        $user_house_data['house_status'] = HouseStatus::Wait;
        $user_house_data['is_history'] = IsHistory::not;

        //取得房产列表
        $user_house_where['user_house_id'] = $user_house_id;
        $user_house_info = IDb::getInstance("asset_house")->setDbWhere($user_house_where)->row();

        //判断是否审核通过
        if(empty($user_house_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("修改房产信息失败！");
            self::send();
        }

        $user_id = $user_house_info['user_id'];

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        //开启事物
        IDb::dbStartTrans();

        //判断是否审核通过
        if($user_house_info['house_status'] == HouseStatus::OK) {
            //取得房产列表
            $house_update_data['is_history'] = IsHistory::yes;
            $house_update_data['update_time'] = get_date_time();
            $user_house_upd = IDb::getInstance("asset_house")->setDbWhere($user_house_where)->setDbData($house_update_data)->upd();
            if($user_house_upd === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0002);
                self::set_msg("修改房产信息失败！");
                self::send();
            }

            //添加房产
            $user_house_data['user_id'] = $user_id;
            $user_house_data['create_time'] = get_date_time();
            $user_house_add = IDb::getInstance("asset_house")->setDbData($user_house_data)->add();
            if($user_house_add === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0003);
                self::set_msg("修改房产信息失败！");
                self::send();
            }
        }else {
            //添加房产
            $user_house_data['update_time'] = get_date_time();
            $user_house_upd = IDb::getInstance("asset_house")->setDbWhere($user_house_where)->setDbData($user_house_data)->upd();
            if ($user_house_upd === false) {
                IDb::dbRollback();
                self::set_code(self::WARNING_0004);
                self::set_msg("修改房产信息失败！");
                self::send();
            }
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_HOUSE,IsHouse::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0005);
            self::set_msg("修改房产信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：删除个人房产
     * @开发者：cxl
     */
    public function house_del(){
        //接收参数
        $user_house_id  = self::get_data('user_house_id',self::NOT_EMPTY,"房产型号编码不能为空！");

        //取得房产列表
        $user_house_where['user_house_id'] = $user_house_id;
        $user_house_info = IDb::getInstance("asset_house")->setDbWhere($user_house_where)->row();

        $user_id = $user_house_info['user_id'];

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        //取得房产列表
        $user_house_data['is_delete'] = IsDelete::Yes;
        $user_house_data['update_time'] = get_date_time();

        IDb::dbStartTrans();
        $user_house_upd = IDb::getInstance("asset_house")->setDbWhere($user_house_where)->setDbData($user_house_data)->upd();

        //判断修改房产是否正常
        if($user_house_upd === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("删除房产信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_HOUSE);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0003);
            self::set_msg("同步房产信息失败！");
            self::send();
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
